import {PureComponent} from "react"
import MSearchForm from "./MSearchForm"
import {connect} from "dva"
import md5 from "md5"
import {getParmaFromUrl,isNull,goTo,handleUrlParams,handleDataType,filterTemplate,goToPage} from "../../utils/helper"
import {ActivityIndicator,Modal} from "antd-mobile"
@connect(({form},{formDataKey})=>{
	return({
		fd:form[formDataKey]||{}
	})
})
export default class MSearchFormWrapper extends PureComponent{
	constructor(props){
		super(props);
		this.formRef="";
		this.modal=null;
		this.enterTime=new Date().getTime();
		this.state={
			showForm:false,
			paramsMsg:"",
		}
	}
	componentWillMount(){
		this.loadConfig()
	}
	loadConfig=()=>{
		let {dispatch,formDataKey,search}=this.props;
		dispatch({
			type:"form/setState",
			payload:{
				[formDataKey]:{}
			}
		});
		let params = getParmaFromUrl(search);
		const {_c = ""} = params;
		if(isNull(_c)){
			this.setState({
				paramsMsg:"缺少_c参数",
				reloadConfig:{
					reload:true,
					reloadFunc:this.loadConfig
				}
			})
		}
		else{
			dispatch({
				type:"form/getFC",
				payload:{
					_c:_c,
					formDataKey:formDataKey,
					callback:(config) =>{
						if(config.error){
							let {code,msg}=config;
							this.setState({
								paramsMsg:<div><span>错误码:{code}</span> <span style={{color:"#aaa"}}>{msg}</span></div>,
								reloadConfig:{
									reload:true,
									reloadFunc:this.loadConfig
								}
							})
						}
						else{
							let {fields}=config.data_store;
							let paramsDefault = handleUrlParams(fields,params);
							let timeSub = new Date().getTime() - this.enterTime;
							let timeOut = 0;
							if(timeSub < 300){
								timeOut = 300 - timeSub;
							}
							setTimeout(() =>{
								this.setState({
									config:config,
									paramsDefault,
									showForm:true
								});
								this.setState({
									empty:1
								})
							},timeOut)
						}
					}
				}
			})
		}
	};
	componentDidMount(){
		
	}
	
	
	handleSubmit=()=>{
		let {fd:{config,formData},dispatch,submitFunc}=this.props;
		let newFormData = handleDataType(config.data_store.fields,formData);
		delete newFormData.id;
		delete newFormData.oid;
		submitFunc&&submitFunc(newFormData)
	};
	render(){
		let {showForm,paramsMsg,paramsDefault,info}=this.state;
		if(!showForm){
			return (
				<div className="normal">
					<ActivityIndicator toast text="正在加载" animating={true}/>
				</div>
			)
		}
		let {formDataKey,fd={},dispatch,listFormData,open}=this.props;
		let {config,formData={}}=fd;
		formData={
			...formData
		};
		const {fields} = config.data_store;
		let allFormData={
			...paramsDefault,
			...info
		};
		let defaultFormData = {};
		fields.map(item =>{
			let itemKey = item.name;
			let defVal=item.default_value;
			let isNumber = item.data_type === "number";
			if(isNull(item.default_value)){
				defaultFormData[itemKey] = defVal
			}
			else{
				if(isNumber){
					if(defVal.toString().indexOf("get")==-1){
						defaultFormData[itemKey] = Number(defVal)
					}
					else{
						defaultFormData[itemKey] = defVal
					}
				}
				else if(item.data_type==="string"){
					defaultFormData[itemKey] = defVal+""
				}
				else if(item.data_type==="array"){
					defaultFormData[itemKey] = defVal
				}
				else{
					defaultFormData[itemKey] = defVal
				}
			}
			if(allFormData[itemKey]!==undefined){
				defaultFormData[itemKey]=allFormData[itemKey]
			}
		});
		let formConfig={
			open,
			listFormData,
			formDataKey,
			submitFunc:this.handleSubmit.bind(this),
			config,
			dispatch,
			defaultFormData,
			formData,
			fd,
			cancelFunc:()=>{
				console.log("取消");
			},
			formRef:this.formRef
		};
		return (
			<MSearchForm ref={(e)=>this.formRef=e} {...formConfig} />
		)
	}
	componentWillUnmount(){
		let {dispatch,formDataKey}=this.props;
		dispatch({
			type:"form/removeState",
			payload:{
				formDataKey
			}
		})
	}
}